package com.woniuxy.movice.handler;




import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.movice.service.RoundService;
import com.woniuxy.movice.util.EmptyUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.woniuxy.movice.dto.Result;
import com.woniuxy.movice.entity.Rounds;
import com.woniuxy.movice.entity.Users;
import com.woniuxy.movice.entity.Vip;
import com.woniuxy.movice.exception.RoundException;


/**
 * @author lxq
 * 选座
 */
@Api("选座接口")
@Controller
public class RoundHandler {

	@Autowired
	private RoundService setingService;
	/**
	 * 
	 * @param pid 场次
	 * @return Result（封装场次对应的座位视图，电影信息，影厅信息）
	 */
	@ApiOperation(value="查询场次信息",notes="产次id")
	@RequestMapping(value="seting/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Result seting(@PathVariable("id")Integer id){
		Result result=new Result();
		try {
			Rounds round=setingService.getSetsInfo(id);
			System.out.println(round);
			result.setFlag(true);
			result.setData(round);
			result.setCode(Result.OK);
		} catch (RoundException e) {
			result.setFlag(false);
			result.setMessage(e.getMessage());
			result.setCode(Result.ERROR);
		}
		return result;
	}
	
	/**
	 * 跳转选择页面
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping(value="cseat/{id}",method=RequestMethod.GET)
	public ModelAndView loadSeatHtml(@PathVariable("id")Integer id,HttpSession session){
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("roundid", id);
		Users user = (Users) session.getAttribute(session.getId());
		if(!EmptyUtil.isEmpty(user)&&!EmptyUtil.isEmpty(user.getAccount())){
			String phone=user.getAccount();
			//将手机号中间四位以*号代替
			String account=phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
			modelAndView.addObject("account", account);
			modelAndView.addObject("islogin",1);
		}else{
			modelAndView.addObject("islogin",2);
		}
		modelAndView.setViewName("seats");
		return modelAndView;
	}
	@RequestMapping(value="login",method=RequestMethod.GET)
	@ResponseBody
	public void testlogin(Users user,HttpSession session){
		if(user.getAccount().equals("18812341234")&&user.getPass().equals("123")){
			user.setUid(1);
			user.setUstate(0);
			user.setVip(new  Vip(1,"1", 1.0));
			session.setAttribute(session.getId(),user);
		}
	}
}
